package com.itbupt.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itbupt.dto.Result;
import com.itbupt.entity.SeckillVoucher;
import com.itbupt.entity.Voucher;
import com.itbupt.mapper.VoucherMapper;
import com.itbupt.service.ISeckillVoucherService;
import com.itbupt.service.IVoucherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author BUPT-Dingchang
 * @Create 2024/5/2 17:15
 * ClassName: VoucherServiceImpl
 * Description:
 */

@Service
public class VoucherServiceImpl extends ServiceImpl<VoucherMapper, Voucher> implements IVoucherService {

    @Autowired
    private ISeckillVoucherService seckillVoucherService;


    /**
     * 查询店铺的优惠券
     * @param shopId    店铺id
     * @return  Result
     */
    @Override
    public Result queryVoucherOfShop(Long shopId) {
        // 查询优惠券信息
        List<Voucher> vouchers = getBaseMapper().queryVoucherOfShop(shopId);
        // 返回结果
        return Result.ok(vouchers);
    }


    /**
     * 添加秒杀优惠券
     * @param voucher   优惠券信息
     */
    @Override
    @Transactional
    public void addSeckillVoucher(Voucher voucher) {
        // 保存优惠券
        save(voucher);
        // 保存秒杀信息
        SeckillVoucher seckillVoucher = new SeckillVoucher();
        seckillVoucher.setVoucherId(voucher.getId());
        seckillVoucher.setStock(voucher.getStock());
        seckillVoucher.setBeginTime(voucher.getBeginTime());
        seckillVoucher.setEndTime(voucher.getEndTime());
        seckillVoucherService.save(seckillVoucher);

    }
}
